package com.iqiyi.pps.epg.api.server.proxy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.api.aop.annotation.SkipAuthCheck;
import com.iqiyi.pps.epg.api.aop.annotation.SkipInterfaceLog;
import com.iqiyi.pps.epg.api.model.web.encode.TEncodeServer;
import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.stream.*;
import com.iqiyi.pps.epg.api.server.web.encode.EncodeServerWebService;
import com.iqiyi.pps.epg.api.server.web.stream.ChannelStreamWebService;
import com.iqiyi.pps.epg.api.server.web.stream.LiveStreamWebService;
import com.iqiyi.pps.epg.api.utils.Constants;
import com.iqiyi.pps.epg.api.utils.RequestUtils;
import com.iqiyi.pps.epg.api.utils.ServiceUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by karkarrotxu on 2016/9/20.
 */
@Service
public class StreamProxy extends BaseProxy {
    private static Logger logger = LoggerFactory.getLogger(StreamProxy.class);

    // 频道流管理
    public String createStream(String fatherId, String streamName, String streamIp, String streamType, String peerCount,
                               String bcsNum, String cutterIdFlv, String cutterIdTs, String dest, int streamApiVersion,
                               String dolbyStreamName, String dolbyStreamUrl, String dolbyAudioChannel, int encodeType, String streamAddress, int playType, int backupEachOther) {
        try {
            if ( StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName) || StringUtils.isEmpty(streamType) || StringUtils.isEmpty(peerCount) ||
                    !StringUtils.isNumeric(bcsNum) || !StringUtils.isNumeric(cutterIdFlv) || !StringUtils.isNumeric(cutterIdTs)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            // 0 h264 1 h265
            if(encodeType == 0 && StringUtils.isEmpty(streamIp) ){
                return ServiceUtil.getResponseError(PARAMETER_ERROR+":streamIp is empty");
            }
            ChannelStreamWebService.Iface service = getChannelStreamWebService();
            String result = service.create(Long.parseLong(fatherId), streamName, streamType, streamIp,
                    Integer.parseInt(peerCount), Integer.parseInt(bcsNum), Integer.parseInt(cutterIdFlv), Integer.parseInt(cutterIdTs),
                    dest, streamApiVersion, dolbyStreamName, dolbyStreamUrl, dolbyAudioChannel, encodeType, streamAddress, playType, backupEachOther);
            //记录用户操作日志
            if (!Constants.CODE_ERROR.equals(result)) {
                writeChannelLog(Long.parseLong(fatherId), 200, "", Constants.getCode(result) + "|" + getStrParam());
            }
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[createStream error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }

    }

    public String deleteStreamById(Long id) {
        try {
            if (id == null) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            ChannelStreamWebService.Iface service = getChannelStreamWebService();
            TChannelStream tChannelStream = service.getById(id);
            String result = service.deleteById(id);
            //记录用户操作日志
            if (Constants.CODE_SUC.equals(result)) {
                writeChannelLog(tChannelStream.getFatherId(), 202, "", Constants.getCode(result) +"|[id="+id+ "][streamName="+tChannelStream.getStreamName()+"]"+ getStrParam());
            }
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[deleteStreamById][id=" + id + "]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    @SkipInterfaceLog
    public String getStreamList(String fatherId) {
        try {
            if (StringUtils.isEmpty(fatherId)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            ChannelStreamWebService.Iface service = getChannelStreamWebService();
            List<TChannelStream> tChannelStreams = service.getList(Long.parseLong(fatherId));
            if (tChannelStreams != null) {
                return Constants.getResponseList(Constants.CODE_SUC, tChannelStreams, "获取成功").toJsonString();
            } else {
                return ServiceUtil.getResponseFail("获取失败");
            }
        } catch (Exception e) {
            logger.error("[getStreamList][fatherId=" + fatherId + "]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String editStream(String id, String fatherId, String streamIp, String streamType, String peerCount, String bcsNum,
                             String cutterIdFlv, String cutterIdTs, String isEnable, String dest, int streamApiVersion, int playType, int backupEachOther) {

        try {
            if (StringUtils.isEmpty(streamType) || StringUtils.isEmpty(peerCount) || StringUtils.isEmpty(isEnable) ||
                    !StringUtils.isNumeric(bcsNum) || !StringUtils.isNumeric(cutterIdFlv) || !StringUtils.isNumeric(cutterIdTs)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            TChannelStream tChannelStream = new TChannelStream();
            tChannelStream.setId(Long.parseLong(id));
            tChannelStream.setFatherId(Long.parseLong(fatherId));
            tChannelStream.setStreamIp(streamIp);
            tChannelStream.setStreamType(streamType);
            tChannelStream.setPeerCount(Integer.parseInt(peerCount));
            tChannelStream.setBcsNum(Integer.valueOf(bcsNum));
            tChannelStream.setCutterIdFlv(Integer.valueOf(cutterIdFlv));
            tChannelStream.setCutterIdTs(Integer.valueOf(cutterIdTs));
            tChannelStream.setIsEnable(isEnable);
            tChannelStream.setDest(dest);
            tChannelStream.setStreamApiVersion(streamApiVersion);
            tChannelStream.setPlayType(playType);
            tChannelStream.setBackupEachOther(backupEachOther);
            ChannelStreamWebService.Iface service = getChannelStreamWebService();
            String result = service.edit(tChannelStream);
//            tChannelStream = service.getById(Long.parseLong(id));
            //记录用户操作日志
            if (!Constants.CODE_ERROR.equals(result)) {
                writeChannelLog(Long.parseLong(fatherId), 201, "", Constants.getCode(result) + "|" + getStrParam());
            }
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[editStream][id=" + id + "][fatherId=" + fatherId + "]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String getStreamById(Long id) {
        try {
            if (id == null) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            ChannelStreamWebService.Iface service = getChannelStreamWebService();
            TChannelStream tChannelStream = service.getById(id);

            if (tChannelStream != null) {
                return Constants.getResponse(Constants.CODE_SUC, tChannelStream, "获取成功").toJsonString();
            } else {
                return ServiceUtil.getResponseFail("获取失败");
            }
        } catch (Exception e) {
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    // 获取流名称suggest
    public String getStreamNameSuggest(long fatherId, String streamName) {
        String jsonStrResp = null;
        try {
            ChannelStreamWebService.Iface service = getChannelStreamWebService();
            TStreamInfoSuggest tStreamInfoSuggest = service.streamInfoSuggest(fatherId, streamName);
            if (tStreamInfoSuggest != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tStreamInfoSuggest, Constants.getCode(Constants.CODE_SUC)).toJsonString();
            } else {
                jsonStrResp = ServiceUtil.getResponseError(Constants.getCode(Constants.CODE_ERROR));
            }
        } catch (Exception e) {
            logger.error("[getStreamNameSuggest][error]", e);
            jsonStrResp = ServiceUtil.getResponseError(INTERFACE_ERROR);
            return jsonStrResp;
        }
        return jsonStrResp;
    }

    // 获取LiveStream
    @SkipInterfaceLog
    public String getLiveStream(String fatherId, String streamName, String formatType) {
        try {
            if (StringUtils.isEmpty(fatherId)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            LiveStreamWebService.Iface service = getLiveStreamService();
            List<TLiveStream> tLiveStreams = service.getLiveStream(Long.parseLong(fatherId), streamName, formatType);
            if (tLiveStreams != null) {
                return Constants.getResponseList(Constants.CODE_SUC, tLiveStreams, "获取成功").toJsonString();
            } else {
                return ServiceUtil.getResponseFail("获取失败");
            }
        } catch (Exception e) {
            logger.error("[getLiveStream][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    // 通知 LiveStream 切片服务状态更新
    @SkipInterfaceLog
    public String notifyChange(String user, String streamName, String streamServer, String streamUrl) {

        if (!user.equals("ppslive")) {
            return ServiceUtil.getResponseError(PARAMETER_ERROR);
        }
        try {
            //index: 切片服务器索引; -1 表示还没切换成功
            String[] splitUrls = streamServer.trim().split("=");
            String sliceIndex = "-1";
            if (splitUrls.length > 1) {
                sliceIndex = splitUrls[1];
            }
            //streamName 切割获取
            String subStreamName = streamName.substring(streamName.indexOf("_")+1, streamName.lastIndexOf("_"));
            // 根据 stream_name 和 status > -1 获取没有删除的流信息, 更新 slice_index 和 stream_urls
            TChannelStream tChannelStream = getChannelStreamWebService().notifyChange(subStreamName, sliceIndex);
            if (null != tChannelStream) {
                writeChannelLog(tChannelStream.getFatherId(), 201, "", "流变更投递成功|[streamName="+streamName+"][subStreamName=" + subStreamName + "][streamServer=" + streamServer + "][streamUrl=" + streamUrl + "][sliceIndex=" + sliceIndex + "]" + getStrParam());
                String msg = "通知成功 |[streamName="+streamName+"][subStreamName=" + subStreamName + "][streamServer=" + streamServer + "][streamUrl=" + streamUrl + "][sliceIndex=" + sliceIndex + "]";
                return Constants.getResponse(Constants.CODE_SUC, null, msg).toJsonString();
            } else {
                //writeChannelLog(tChannelStream.getFatherId(), 201, "", "流变更投递成功,但该流已被删除|[subStreamName=" + subStreamName + "][streamServer=" + streamServer + "][streamUrl=" + streamUrl + "][sliceIndex=" + sliceIndex + "]" + getStrParam());
                logger.error("[notifyChange][error]", "流变更投递成功,但该流已被删除|[streamName="+streamName+"][subStreamName=" + subStreamName + "][streamServer=" + streamServer + "][streamUrl=" + streamUrl + "][sliceIndex=" + sliceIndex + "]");
                return ServiceUtil.getResponseFail("通知成功| 该流已从数据库标记为删除 | tChannelStream == null");
            }

        } catch (Exception e) {
            logger.error("[notifyChange][exception]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);

        }

    }

    @SkipInterfaceLog
    @SkipAuthCheck
    public String streamMonitor(JSONObject jsonObject){
//        logger.info("[streamMonitor][info]{}", jsonArray);
        //存入数据库
        try {
            //get

            int srcId = jsonObject.getIntValue("src_id");
            String srcDesc = jsonObject.getString("src_desc");
            int msgId = jsonObject.getIntValue("msg_id");
            String msgDesc = jsonObject.getString("msg_desc");
            String time = jsonObject.getString("time");
            String progName = jsonObject.getString("prog_name");
            String clientHost = jsonObject.getString("clientHost");
            int progNum = jsonObject.getIntValue("prog_num");


            //set
            TLiveStreamMonitor tLiveStreamMonitor = new TLiveStreamMonitor();
            tLiveStreamMonitor.setSrcId(srcId);
            tLiveStreamMonitor.setSrcDesc(srcDesc);
            tLiveStreamMonitor.setMsgId(msgId);
            tLiveStreamMonitor.setMsgDesc(msgDesc);
            tLiveStreamMonitor.setTime(time);
            tLiveStreamMonitor.setProgNum(progNum);
            tLiveStreamMonitor.setProgName(progName);
            tLiveStreamMonitor.setClientHost(clientHost);

            //add
            getLiveStreamService().saveLiveStreamMonitor(tLiveStreamMonitor);
            String msg = "通知成功 | [param =" + jsonObject + "]";
            return Constants.getResponse(Constants.CODE_SUC, null, msg).toJsonString();
        } catch (TException e) {
            logger.error("[streamMonitor][exception]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }


    // FLV频道流管理
    public String createFlv(String fatherId, String streamName, String isDefault, String streamIp) {
        try {
            if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)
                    || StringUtils.isEmpty(isDefault) || StringUtils.isEmpty(streamIp)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }

            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.createFlv(Long.parseLong(fatherId), streamName, isDefault, streamIp);
            //记录用户操作日志
            writeChannelLog(Long.parseLong(fatherId), 210, "", Constants.getCode(result) + "|" + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[createFlv][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String startFlv(String fatherId, String streamName) {
        try {
            if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }

            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.startFlv(Long.parseLong(fatherId), streamName);
            //记录用户操作日志
            if (Constants.CODE_SUC.equals(result)) {
                writeChannelLog(Long.parseLong(fatherId), 211, "", Constants.getCode(result) + "|" + getStrParam());
            }
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[startFlv][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String stopFlv(String fatherId, String streamName) {
        try {
            if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.stopFlv(Long.parseLong(fatherId), streamName, false);
            //记录用户操作日志
            writeChannelLog(Long.parseLong(fatherId), 212, "", Constants.getCode(result) + "|" + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[stopFlv][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String deleteFlv(String fatherId, String streamName) {
        try {
            if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.deleteFlv(Long.parseLong(fatherId), streamName);
            //记录用户操作日志
            if (Constants.CODE_SUC.equals(result)) {
                writeChannelLog(Long.parseLong(fatherId), 213, "", Constants.getCode(result) + "|" + getStrParam());
            }
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[deleteFlv][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String restartFlv(String fatherId, String streamName, String internalIp, String streamIp) {
        try {
            if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName) || StringUtils.isEmpty(internalIp) ||
                    StringUtils.isEmpty(streamIp)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.restartFlv(Long.parseLong(fatherId), streamName, internalIp, streamIp);
            writeChannelLog(Long.parseLong(fatherId), 215, "", Constants.getCode(result) + " | " + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[EPG][API][restartFlv][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    // TS频道流管理
    public String createTs(String fatherId, String streamName, String streamIp) {
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.createTs(Long.parseLong(fatherId), streamName, streamIp);
            //记录用户操作日志
            writeChannelLog(Long.parseLong(fatherId), 220, "", Constants.getCode(result) + "|" + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[createTs][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String startTs(String fatherId, String streamName) {
        try {
            if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.startTs(Long.parseLong(fatherId), streamName);
            //记录用户操作日志
            writeChannelLog(Long.parseLong(fatherId), 221, "", Constants.getCode(result) + "|" + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[startTs][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String stopTs(String fatherId, String streamName) {
        try {
            if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.stopTs(Long.parseLong(fatherId), streamName, false);
            //记录用户操作日志
            writeChannelLog(Long.parseLong(fatherId), 222, "", Constants.getCode(result) + "|" + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[stopTs][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String deleteTs(String fatherId, String streamName) {
        try {
            if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }

            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.deleteTs(Long.parseLong(fatherId), streamName);
            //记录用户操作日志
            writeChannelLog(Long.parseLong(fatherId), 223, "", Constants.getCode(result) + "|" + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[deleteTs][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String restartTs(String fatherId, String streamName, String internalIp, String streamIp) {
        try {
            if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName) || StringUtils.isEmpty(internalIp) ||
                    StringUtils.isEmpty(streamIp)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.restartTs(Long.parseLong(fatherId), streamName, internalIp, streamIp);
            writeChannelLog(Long.parseLong(fatherId), 225, "", Constants.getCode(result) + " | " + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[restartTs][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }


    public String encodeServerStat(String serverIp, String signalDesc, String inputSignal, String outputSignal, String extraData, int encodeType, int status) {
        try {
            if (null == serverIp || null == signalDesc || null == inputSignal || null == outputSignal) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            int is_dolby = 0;
            String audio = "";
            if (status != -1 && StringUtils.isNotEmpty(extraData)) {
                extraData = extraData.replace("\\:", ":");
                JSONObject info = JSONObject.parseObject(extraData);
                if (null != info && info.containsKey("audio")) {
                    audio = info.getString("audio").toLowerCase();
                    if (audio.equals("ac3") || audio.equals("copy")||audio.equals("ec3")) {
                        is_dolby = 1;
                    }
                }
            }
            TEncodeServer encodeServer = new TEncodeServer();
            encodeServer.setServerIp(serverIp);
            encodeServer.setSignalDesc(signalDesc);
            encodeServer.setInputSignal(inputSignal);
            encodeServer.setOutputSignal(outputSignal);
            encodeServer.setStatus(status);
            encodeServer.setCreateTime(System.currentTimeMillis() + "");
            encodeServer.setModifyTime(System.currentTimeMillis() + "");
            encodeServer.setEncodeType(encodeType);
            encodeServer.setExtraData(extraData);
            encodeServer.setIsDolby(is_dolby);
            encodeServer.setAudio(audio);
            EncodeServerWebService.Iface service = getEncodeServerService();
            long result = service.create(encodeServer);
            if (result > 0) {
                return Constants.getJsonResponse(Constants.CODE_SUC, null, "更新成功");
            } else {
                return ServiceUtil.getResponseError("更新失败:" + result);
            }
        } catch (Exception e) {
            logger.error("[encodeServerStat][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    @SkipAuthCheck
    public String getCutterIdInfo(int maxVal, String cutterType, String sortType) {
        try {
            cutterType = (null == cutterType || "".equals(cutterType)) ? "FLV" : cutterType.toUpperCase();
            if (!"FLV".equals(cutterType) && !"TS".equals(cutterType)) {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
            LiveStreamWebService.Iface service = getLiveStreamService();
            List<Map<String, String>> result = service.getCutterIdInfo(maxVal, null == sortType ? "asc" : sortType, cutterType);

            if (result != null) {
                return Constants.getJsonResponse(Constants.CODE_SUC, result, null);
            } else {
                return ServiceUtil.getResponseFail("获取失败");
            }
        } catch (Exception e) {
            logger.error("[getCutterIdInfo]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String createDolby(String fatherId, String streamName) {
        if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
            return ServiceUtil.getResponseError(PARAMETER_ERROR);
        }
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.createDolby(Long.parseLong(fatherId), streamName);
            //记录用户操作日志
            writeChannelLog(Long.parseLong(fatherId), Constants.LogType.DOLBY_CREATE.getIndex(), "",
                    Constants.getCode(result) + "|" + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[createDolby][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }


    public String startDolby(String fatherId, String streamName) {
        if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
            return ServiceUtil.getResponseError(PARAMETER_ERROR);
        }
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.startDolby(Long.parseLong(fatherId), streamName);
            //记录用户操作日志
            if (Constants.CODE_SUC.equals(result)) {
                writeChannelLog(Long.parseLong(fatherId), Constants.LogType.DOLBY_START.getIndex(), "", Constants.getCode(result) + "|" + getStrParam());
            }
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[startDolby][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }


    public String stopDolby(String fatherId, String streamName) {
        if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
            return ServiceUtil.getResponseError(PARAMETER_ERROR);
        }
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.stopDolby(Long.parseLong(fatherId), streamName, false);
            //记录用户操作日志
//            if (Constants.CODE_SUC.equals(result)) {
            writeChannelLog(Long.parseLong(fatherId), Constants.LogType.DOLBY_STOP.getIndex(), "", Constants.getCode(result) + "|" + getStrParam());
//            }
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[stopDolby][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }

    }

    public String deleteDolby(String fatherId, String streamName) {
        if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
            return ServiceUtil.getResponseError(PARAMETER_ERROR);
        }
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.deleteDolby(Long.parseLong(fatherId), streamName);
            //记录用户操作日志
            if (Constants.CODE_SUC.equals(result)) {
                writeChannelLog(Long.parseLong(fatherId), Constants.LogType.DOLBY_DELETE.getIndex(), "", Constants.getCode(result) + "|" + getStrParam());
            }
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[deleteDolby][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    public String restartDolby(String fatherId, String streamName) {
        if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
            return ServiceUtil.getResponseError(PARAMETER_ERROR);
        }
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.restartDolby(Long.parseLong(fatherId), streamName);
            writeChannelLog(Long.parseLong(fatherId), Constants.LogType.DOLBY_RESTART.getIndex(), "", Constants.getCode(result) + " | " + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[restartDolby][error]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    @SkipInterfaceLog
    public String getNetMonitorInfo(long channelQipuId) {
        try {
            List<TNetMonitorInfo> result = getLiveStreamService().getNetMonitorInfo(channelQipuId);
            if (null != result) {
                return Constants.getResponseList(Constants.CODE_SUC, result, null).toJsonString();
            } else {
                return ServiceUtil.getResponseError(PARAMETER_ERROR);
            }
        } catch (Exception e) {
            logger.error("[getNetMonitorInfo]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    @SkipInterfaceLog
    public String getDuBiStreamNameSuggest(String streamName, int encodeType) {
        try {
            EncodeServerWebService.Iface service = getEncodeServerService();
            List<TEncodeServer> tEncodeServer = service.duBiStreamInfoSuggest(streamName, encodeType);
            if (tEncodeServer != null) {
                return Constants.getResponseList(Constants.CODE_SUC, tEncodeServer, "获取成功").toJsonString();
            } else {
                return ServiceUtil.getResponseError(Constants.getCode(Constants.CODE_ERROR));
            }
        } catch (Exception e) {
            logger.error("[getDuBiStreamNameSuggest]", e);
            return ServiceUtil.getResponseError(INTERFACE_ERROR);
        }
    }

    @SkipInterfaceLog
    public String geth265StreamNameSuggest(String streamName) {
        try {
            List<TEncodeServer> tEncodeServer = getEncodeServerService().h254StreamInfoSuggest(streamName);
            if (tEncodeServer != null) {
                return Constants.getResponseList(Constants.CODE_SUC, tEncodeServer, "获取成功").toJsonString();
            } else {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, Constants.getCode(Constants.CODE_ERROR));
            }
        } catch (Exception e) {
            logger.error("[EPG][API][geth265StreamNameSuggest]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }

    public String createNewTs(String fatherId, String streamName) {
        if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
        }
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.createNewTs(Long.parseLong(fatherId), streamName);
            //记录用户操作日志
            writeChannelLog(Long.parseLong(fatherId), Constants.LogType.NEWTS_CREATE.getIndex(), "",
                    Constants.getCode(result) + "|" + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[EPG][API][createNewTs][error]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }

    public String startNewTs(String fatherId, String streamName) {
        if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
        }
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.startNewTs(Long.parseLong(fatherId), streamName);
            if (Constants.CODE_SUC.equals(result)) {
                writeChannelLog(Long.parseLong(fatherId), Constants.LogType.NEWTS_START.getIndex(), "", Constants.getCode(result) + "|" + getStrParam());
            }
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[EPG][API][startNewTs][error]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }

    public String stopNewTs(String fatherId, String streamName) {
        if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
        }
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.stopNewTs(Long.parseLong(fatherId), streamName, false);
            writeChannelLog(Long.parseLong(fatherId), Constants.LogType.NEWTS_STOP.getIndex(), "", Constants.getCode(result) + "|" + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[EPG][API][stopNewTs][error]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }

    public String deleteNewTs(String fatherId, String streamName) {
        if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
        }
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.deleteNewTs(Long.parseLong(fatherId), streamName);
            //记录用户操作日志
            if (Constants.CODE_SUC.equals(result)) {
                writeChannelLog(Long.parseLong(fatherId), Constants.LogType.NEWTS_DELETE.getIndex(), "", Constants.getCode(result) + "|" + getStrParam());
            }
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[EPG][API][deleteNewTs][error]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }

    public String restartNewTs(String fatherId, String streamName) {
        if (StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(streamName)) {
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
        }
        try {
            LiveStreamWebService.Iface service = getLiveStreamService();
            String result = service.restartNewTs(Long.parseLong(fatherId), streamName);
            writeChannelLog(Long.parseLong(fatherId), Constants.LogType.NEWTS_RESTART.getIndex(), "", Constants.getCode(result) + " | " + getStrParam());
            return Constants.getJsonResponse(result, null, Constants.getCode(result));
        } catch (Exception e) {
            logger.error("[EPG][API][restartNewTs][error]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }

    }

    @SkipInterfaceLog
    public String getStreamSearchList(String fatherId, String streamName, Integer pageNo, Integer pageSize){
        try{
            Map<String, String> params = new HashMap<>();
            TFilter tFilter = new TFilter();
            if (StringUtils.isNotBlank(fatherId)) {
                params.put("EQL_fatherId", fatherId);
            }

            if (StringUtils.isNotBlank(streamName)) {
                params.put("LIKES_streamName", streamName);
            }
            params.put("GTI_status", "-1");
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "id", "desc", true);

            TChannelStreamPage tLiveStreamPage =ServiceUtil. getChannelStreamWebService().getSearchList(page, tFilter);

            if (tLiveStreamPage != null) {
                return Constants.getResponse(Constants.CODE_SUC, tLiveStreamPage, "操作成功").toJsonString();
            } else {
                return  Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
            }
        }catch (Exception e){
            logger.error("[EPG][API][getStreamSearchList][error]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }

    public String switchBackup(String fatherId, String qipuId) {
        if (StringUtils.isBlank(fatherId)) {
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
        }

        try {
            ChannelStreamWebService.Iface service = ServiceUtil.getChannelStreamWebService();
            int result = service.switchBackup(Long.parseLong(fatherId), Long.parseLong(qipuId), ServiceUtil.getOpUser());

            if (result == 0) {
//                writeChannelLog(Long.parseLong(fatherId), Constants.LogType.ONKEY_SWITCH_STREAM.getIndex(), ServiceUtil.getOpUser(), "操作成功");
                return Constants.getResponse(Constants.CODE_SUC, null, "操作成功").toJsonString();
            }else{
                writeChannelLog(Long.parseLong(fatherId), Constants.LogType.ONKEY_SWITCH_STREAM.getIndex(), ServiceUtil.getOpUser(), "操作失败");
                return Constants.getJsonResponse(Constants.CODE_ERROR, result, "操作失败");
            }
        } catch (Exception e) {
            logger.error("[EPG][API][switchBackup][error]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }
}
